const { query } = require('./config/db');

async function debugDatabase() {
  try {
    console.log('=== 调试数据库数据 ===');
    
    // 获取原始数据，不做任何处理
    const result = await query('SELECT id, name, service_facilities, business_documents, image_urls FROM parking_lots WHERE id = 1');
    
    if (result.length > 0) {
      const lot = result[0];
      console.log('原始数据:');
      console.log('ID:', lot.id);
      console.log('Name:', lot.name);
      console.log('service_facilities 类型:', typeof lot.service_facilities);
      console.log('service_facilities 值:', lot.service_facilities);
      console.log('service_facilities 长度:', lot.service_facilities ? lot.service_facilities.length : 'null/undefined');
      console.log('business_documents 类型:', typeof lot.business_documents);
      console.log('business_documents 值:', lot.business_documents);
      console.log('image_urls 类型:', typeof lot.image_urls);
      console.log('image_urls 值:', lot.image_urls);
      
      // 尝试用不同方式处理
      console.log('\n=== 尝试处理数据 ===');
      
      // 直接强制更新
      await query('UPDATE parking_lots SET service_facilities = ?, business_documents = ?, image_urls = ? WHERE id = 1', [
        JSON.stringify([]),
        JSON.stringify({}),
        JSON.stringify([])
      ]);
      
      console.log('强制更新完成');
      
      // 再次检查
      const after = await query('SELECT service_facilities, business_documents, image_urls FROM parking_lots WHERE id = 1');
      console.log('\n更新后:');
      console.log('service_facilities:', after[0].service_facilities);
      console.log('business_documents:', after[0].business_documents);
      console.log('image_urls:', after[0].image_urls);
      
      // 测试JSON解析
      try {
        JSON.parse(after[0].service_facilities);
        JSON.parse(after[0].business_documents);
        JSON.parse(after[0].image_urls);
        console.log('✅ JSON解析成功！');
      } catch (e) {
        console.log('❌ JSON解析失败:', e.message);
        
        // 查看字符码
        const sf = after[0].service_facilities;
        console.log('service_facilities 字符码:');
        for (let i = 0; i < Math.min(sf.length, 20); i++) {
          console.log(`  ${i}: '${sf[i]}' (${sf.charCodeAt(i)})`);
        }
      }
    }
    
  } catch (error) {
    console.error('调试失败:', error);
  }
  
  process.exit(0);
}

debugDatabase();